package com.drug.mingkang.controller;
import com.drug.mingkang.model.HqRawmaterialType;
import com.drug.mingkang.service.HqRawmaterialTypeService;
import com.drug.mingkang.utils.ProductTypeParent;
import com.drug.mingkang.utils.ResponseMessage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * (HqRawmaterialType)表控制层
 *
 * @author ll
 * @since 2020-08-10 15:19:45
 */
@RestController
@RequestMapping("hqRawmaterialType")
public class HqRawmaterialTypeController {
    /**
     * 服务对象
     */
    @Resource
    private HqRawmaterialTypeService hqRawmaterialTypeService;

    ResponseMessage responseMessage = new ResponseMessage();


    /**
     *  查询类型
     * @param
     * @param
     * @return
     */
    @RequestMapping("selectAe")
    @ResponseBody
    public Map<String,Object> selectA(int page,int limit,String typeName ,String filterRules,String sort ){
        HqRawmaterialType type=new HqRawmaterialType();
        type.setTypeName(typeName);
        Map<String , Object> map = new HashMap<>();
        List<HqRawmaterialType> list = hqRawmaterialTypeService.queryAllByLimit(type);
        map.put("code",0);
        map.put("count", hqRawmaterialTypeService.selectCount(type));
        map.put("data",list);
        map.put("msg","查找成功");
        return  map ;
    }

    @RequestMapping("delete")
    public ResponseMessage delete(String id){
        List<HqRawmaterialType>  type = hqRawmaterialTypeService.selectDlt(id);
        if(type.size()>0){
            responseMessage.setStatus(0);
            responseMessage.setMessage("此材料正在使用中不能删除");
        }else if (hqRawmaterialTypeService.deleteById(id)>0){
            responseMessage.setMessage("删除成功");
            responseMessage.setStatus(1);
        }else {
            responseMessage.setStatus(0);
            responseMessage.setMessage("删除失败");
        }
        return responseMessage;
    }

    @RequestMapping("insert")
    public ResponseMessage insert(HqRawmaterialType hqRawmaterialType){
        int rows = 0;
        if (hqRawmaterialType.getTypeId()!=null && !hqRawmaterialType.getTypeId().equals("")){
            rows = hqRawmaterialTypeService.update(hqRawmaterialType);
        }else {
            HqRawmaterialType  s =  hqRawmaterialTypeService.selectName(hqRawmaterialType);
            hqRawmaterialType.setTypeId(UUID.randomUUID().toString());
            hqRawmaterialType.setOperator("LL");
            hqRawmaterialType.setSort(1);
            if (s!=null){
                rows=100;
                 responseMessage.setStatus(0);
                 responseMessage.setMessage("已有此类型");
            }else{
                rows = hqRawmaterialTypeService.insert(hqRawmaterialType);
            }

        }
        if(rows==100){
            responseMessage.setStatus(2);
            responseMessage.setMessage("已有此类型");
        }
        if(rows>0){
            responseMessage.setMessage("操作成功");
            responseMessage.setStatus(1);
        }else {
            responseMessage.setStatus(0);
            responseMessage.setMessage("操作失败");
        }
        return responseMessage;
    }

    /**
     * 查询所有类型
     * @param HqRawmaterialType
     * @return
     */
    @GetMapping("selectTypeName")
    public List<HqRawmaterialType> selectTypeName(HqRawmaterialType HqRawmaterialType){
        List<HqRawmaterialType> list = hqRawmaterialTypeService.queryAll(HqRawmaterialType);
        return list ;
    }


    /**
     * 默认加载
     */
    public static String url = "https://wenku.baidu.com/view/666558bdfd0a79563c1e72a8.html";
    @RequestMapping("add")
    public void add() {
        try {
            Document doc = Jsoup.connect(url).get();
            Elements ele = doc.select("p");
            for (Element e : ele) {
                String fname = e.text();
                System.out.println("-------"+fname);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Document doc = Jsoup.connect(url).get();
            Elements ele = doc.select(".ie-fix");
            for (Element e : ele) {
                String fname = e.text();
                System.out.println("-------"+fname);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 类型树状
     * @return
     */
    @PostMapping("getTypeTree")
    public Map<String,Object> getTree(){
        System.out.println("树状");
        List<ProductTypeParent> list = hqRawmaterialTypeService.getTree();
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        map.put("code",0);
        return map;
    }

}